package com.common.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.common.pager.PageHelper;

public class DB4MapUtil extends DBBaseUtil {
	private static Logger log = Logger.getLogger(DB4MapUtil.class);
	
	/**
	 * @param sql
	 * @param pager
	 * @param objects
	 * @return
	 * @throws NumberFormatException
	 * @throws Exception
	 */
	public static Vector executePager4Map(String sql, PageHelper pager,Object...objects) throws NumberFormatException, Exception {
		
		Connection conn = DBSource.getConnection();
		
		pager.setTotalCount(Integer.parseInt(String.valueOf(getObjectSingleByConn(conn,SqlUtil.getAnalyseSQLOfcount(sql),objects))));
		boolean hasOffset = pager.getCurrentPage()>1?true:false;
		sql = SqlUtil.getSqlLimit(sql, hasOffset, (pager.getCurrentPage() - 1) * pager.getRowPage(), pager.getRowPage());
		
		Vector<Hashtable> vResult = new Vector<Hashtable>();
		try {
			vResult = execute4MapByConn(conn,sql,objects);
		} catch (Exception sqle)
		{
			log.error(sqle.getMessage()+"sql 发生异常",sqle);
		} finally {
			closeConnection(conn,null,null );
		}
		return vResult;
	}
	
	/**
	 * @param strSql
	 * @param objects
	 * @return
	 * @throws Exception
	 */
	public static Vector<Hashtable> executeTop4Map(String strSql,int rows, Object... objects )  {
		strSql = SqlUtil.getSqlLimit(strSql, false,0, rows);
		return execute4Map(strSql,objects);// 返回值

	}
	/**
	 * @param strSql
	 * @param objects
	 * @return
	 * @throws Exception
	 */
	public static Vector<Hashtable> execute4Map(String strSql, Object... objects )  {
		Connection conn = DBSource.getConnection();
		Vector<Hashtable> vResult = new Vector<Hashtable>();
		try {
			vResult = execute4MapByConn(conn,strSql,objects);
		} catch (Exception sqle)
		{
			log.error(sqle.getMessage()+"sql 发生异常",sqle);
		} finally {
			closeConnection(conn,null,null );
		}
		return vResult;// 返回值

	}
	
	public static Vector<Hashtable> execute4MapByConn(Connection conn,String strSql, Object... objects )  {
		PreparedStatement st = null;// 执行语句
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		Vector<Hashtable> vResult = new Vector<Hashtable>();
		try {
			st = conn.prepareStatement(strSql);// 创建执行语句
			for(int i=0;i<objects.length;i++){
				int n = i+1;
				setStatemnt(st, n, objects[i]);
			}
			rs = st.executeQuery();// 执行查询
			rsmd = rs.getMetaData(); // 元数据
			int nFieldCount = rsmd.getColumnCount();// 到得字段列数
			while (rs.next()) {
				Hashtable hRecord = new Hashtable();
				for (int k = 1; k <= nFieldCount; k++) {
					String strFieldName = rsmd.getColumnName(k);

					Object strTemp = rs.getObject(strFieldName);
					if (strTemp == null) {
						strTemp = "";
					}
					hRecord.put(strFieldName, strTemp);
				}
				vResult.addElement(hRecord);
			}
		} catch (SQLException sqle)
		{
			log.error(sqle.getMessage()+"sql 发生异常",sqle);
		} finally {
			closeConnection(null,st,rs );
		}
		return vResult;// 返回值

	}
	
}
